package com.tencent.tbds.demo.hive;

import joptsimple.OptionParser;
import joptsimple.OptionSet;
import joptsimple.OptionSpec;

import java.io.IOException;

public class HiveKerberosDemoOption {
    private final OptionParser optionParser;
    private final OptionSet optionSet;
    private final OptionSpec<String> zkList;
    private final OptionSpec<String> principal;
    private final OptionSpec<String> keytab;
    private final OptionSpec<String> defaultFS;

    public HiveKerberosDemoOption(String[] args) {
        this.optionParser = new OptionParser();

        zkList = optionParser.accepts("zk-list")
                .withRequiredArg()
                .required()
                .describedAs("the zookeeper connection in the form host1:port1,host2:port2");

        principal = optionParser.accepts("principal")
                .withRequiredArg()
                .required()
                .describedAs("kerberos authentication principal");

        keytab = optionParser.accepts("keytab")
                .withRequiredArg()
                .required()
                .describedAs("kerberos authentication keytab file path");

        defaultFS = optionParser.accepts("default-fs")
                .withRequiredArg()
                .defaultsTo("hdfs://hdfsCluster")
                .describedAs("file system url, default is hdfs://hdfsCluster");

        optionParser.accepts("help").forHelp();
        this.optionSet = optionParser.parse(args);
    }

    public boolean hasHelp() {
        return optionSet.has("help");
    }

    public void printHelp() throws IOException {
        optionParser.printHelpOn(System.out);
    }

    public OptionParser getOptionParser() {
        return optionParser;
    }

    public OptionSet getOptionSet() {
        return optionSet;
    }

    public String getZkList() {
        return optionSet.valueOf(zkList);
    }

    public String getPrincipal() {
        return optionSet.valueOf(principal);
    }

    public String getKeytab() {
        return optionSet.valueOf(keytab);
    }

    public String getDefaultFS() {
        return optionSet.valueOf(defaultFS);
    }
}
